<%@ page language="java" contentType="text/html;charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="com.yuxin.common.util.CollectionUtils" %>
<%@ page import="com.yuxin.common.util.DBUtil" %>
<%@ page import="com.yuxin.common.util.JsonUtil" %>
<%@ page import="com.yuxin.common.util.UUIDUtil" %>
<%@ page import="com.yuxin.common.util.StringUtils" %>
<%@ page import="java.sql.Connection" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.Set" %>
<%@ page import="com.yuxin.common.util.*" %>
<%@ page import="com.yuxin.core.TransactionManager" %>
<%@ page import="com.yuxin.core.exception.BusinessException" %>

<%
    String action = request.getParameter("action");
    Map result = new HashMap();
    response.setContentType("text/html;charset=UTF-8");
    Connection conn = DBUtil.getConnection();
    TransactionManager transaction = new TransactionManager(conn);
    try{
       if("add".equals(action)){
           transaction.begin();
		   Map<String,String> uploadSet = new HashMap<String,String>();
		   uploadSet.put("type",FileUploadUtil.COMPRESS_SMALL_IMAGE);
		   uploadSet.put("width","136");
		   uploadSet.put("height","100");
           Map uploadResult = FileUploadUtil.upload(request,uploadSet);
           List<Map> fileList = (List<Map>)uploadResult.get("files");
           Map formData = (Map)uploadResult.get("formData");
           if(!CollectionUtils.isEmpty(fileList)){
               for (Map file : fileList){
                   formData.put(file.get("filed"),file.get("path"));
               }
           }
           if(!StringUtils.hasText(formData.get("IMG")) && !StringUtils.hasText(formData.get("imgPath"))){
               formData.remove("IMG");
           }
           formData.remove("imgPath");
           if(StringUtils.hasText(formData.get("ID") + "")){
               DBUtil.update(conn,formData,"P_NOTE");
           }else{
               formData.put("ID",UUIDUtil.getUUID());
               DBUtil.save(conn,formData,"P_NOTE");
           }
           //request.getRequestDispatcher("admin/message_l.jsp").forward(request, response);
           response.sendRedirect("message_l.jsp");
        }else if("delete".equals(action)){
            String id = request.getParameter("ID");
            if(StringUtils.hasText(id)){
            	transaction.begin();
                DBUtil.executeSql(conn,"DELETE FROM P_NOTE WHERE ID = ? ",id);
            }
            result.put("isSuccess",true);
            response.getWriter().write(JsonUtil.convertString(result));
            out.clear();
            out = pageContext.pushBody();
        }
        
    }catch (Exception e){
        transaction.rollback();
        e.printStackTrace();
        result.put("isSuccess",false);
        result.put("error",e.getMessage());
        throw new BusinessException(e);
    }finally {
        transaction.commit();
        DBUtil.close(conn);
        request.setAttribute("result", result);
    }
%>